//
//  YZMStockInfoView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/25.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockInfoView.h"
#import "YZMStockRealModel.h"
#import "YZMyStocksService.h"
#import "NSNumber+Utils.h"
#import "YZMConfigureService.h"

#import <BlocksKit/UIControl+BlocksKit.h>

@interface YZMStockInfoView()

@property (nonatomic, strong) UILabel *nameLB;
@property (nonatomic, strong) UIButton *operationBT;
@property (nonatomic, strong) UILabel *codeLB;
@property (nonatomic, strong) UILabel *valueLB;
@property (nonatomic, strong) UILabel *changeLB;
@property (nonatomic, strong) UILabel *highRiskLB;

@end

@implementation YZMStockInfoView

- (instancetype)init{
    
    self = [super initWithFrame:CGRectMake(0, 0, 1, 1)];
    if (self) {
        
        self.backgroundColor = [YZMAppStyle colorForMain];
        [self createObjects];
    }
    
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [YZMAppStyle colorForMain];
    [self createObjects];
}


- (void)updateWithStockCode:(NSString *)stockCode andStockName:(NSString *)stockName{
    
    self.nameLB.text = stockName;
    self.codeLB.text = stockCode;
    self.operationBT.selected = [[YZMyStocksService sharedService] isStockExist:stockCode];
    self.valueLB.text = @"--";
    self.changeLB.text = @"--   --";
    self.highRiskLB.hidden = YES;
    
    self.valueLB.textColor = [YZMAppStyle colorForGrayText];
    self.changeLB.textColor = [YZMAppStyle colorForGrayText];
}


- (void)setRealStock:(YZMStockRealModel *)realStock{
    
    _realStock = realStock;
    self.nameLB.text = realStock.name;
    if ( [@(realStock.price) isEqualWithDouble:0.0] ) {
        
        self.valueLB.text = @(realStock.lastClosePrice).p02fString;
        self.highRiskLB.hidden = NO;
        self.highRiskLB.text = @"停牌股";
        return;
    }
    
    self.valueLB.text = @(realStock.price).p02fString;
    double distance = realStock.price - realStock.lastClosePrice;
    if ([@(distance) isEqualWithDouble:0.0]) {
        
        self.valueLB.textColor = [YZMAppStyle colorForGrayText];
        self.changeLB.textColor = [YZMAppStyle colorForGrayText];
        self.changeLB.text = @"+0.00   +0.00%";
        
    }else if([@(distance) isGreaterThanDouble:0.0]){
        
        self.valueLB.textColor = [YZMAppStyle colorForRiseText];
        self.changeLB.textColor = [YZMAppStyle colorForRiseText];
        self.changeLB.text = [NSString stringWithFormat:@"+%@   +%@%%",@(distance).p02fString, @(distance*100/realStock.lastClosePrice).p02fString];
        
    }else{
        self.valueLB.textColor = [YZMAppStyle colorForDropText];
        self.changeLB.textColor = [YZMAppStyle colorForDropText];
        self.changeLB.text = [NSString stringWithFormat:@"%@   %@%%",@(distance).p02fString, @(distance*100/realStock.lastClosePrice).p02fString];
    }
    
    self.highRiskLB.hidden = ![[YZMConfigureService sharedService] isInLimitStockListWithStockCode:realStock.code];
    self.highRiskLB.text = @"风险股";
}

- (void)formatColorWithLabel:(UILabel *)label andGreateDouble:(double)greaterDouble andSmallDouble:(double)smallDouble{
    
    if ( [@(greaterDouble) isGreaterThanDouble:smallDouble] ) {
        
        label.textColor = [YZMAppStyle colorForRiseText];
        
    }else if( [@(greaterDouble) isEqualWithDouble:smallDouble] ){
        
        label.textColor = [YZMAppStyle colorForGrayText];
        
    }else{
        
        label.textColor = [YZMAppStyle colorForDropText];
    }
}

#pragma mark -- ui

- (void)createObjects{
    
    //股票名称
    self.nameLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:18] andColor:[YZMAppStyle colorForMainTextColor]];
    self.nameLB.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.nameLB];
    [self.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self).offset(20);
    }];
    
    //添加删除操作
    self.operationBT = [UIButton new];
    [self.operationBT setImage:[UIImage imageNamed:@"icon_stock_add"] forState:UIControlStateNormal];
    [self.operationBT setImage:[UIImage imageNamed:@"icon_stock_delete"] forState:UIControlStateSelected];
    [self.operationBT bk_addEventHandler:^(UIButton *sender) {
        
        if (_operationAction) {
            
            BOOL result = _operationAction(!sender.selected);
            if (result) {
                sender.selected = !sender.selected;
                sender.selected ? [MBProgressHUD showMessage: @"已添加至自选"] : [MBProgressHUD showMessage: @"已从自选删除"];
            }
        }
    } forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.operationBT];
    [self.operationBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLB.mas_right).offset(5);
        make.centerY.equalTo(self.nameLB);
        make.width.height.mas_equalTo(24);
    }];
    
    //更换股票操作
    UIButton *changeBT = [UIButton new];
    [YZMSetBorderAndCornerRadius setView:changeBT borderWidth:1.5 borderColor:[YZMAppStyle colorForYellowButton] cornerRadius:5.0];
    changeBT.titleLabel.font = [YZMAppStyle mediumSizeBoldFont];
//    [changeBT setImage:[UIImage imageNamed:@"icon_stock_search_white"] forState:UIControlStateNormal];
    [changeBT setTitle:@"更换股票" forState:UIControlStateNormal];
    [changeBT setTitleColor:[YZMAppStyle colorForYellowButton] forState:UIControlStateNormal];
    [changeBT bk_addEventHandler:^(id sender) {
        if (_changeAction) {
            
            _changeAction();
        }
    } forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:changeBT];
    [changeBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-15);
        make.centerY.equalTo(self);
        make.width.mas_offset(70);
        make.height.mas_offset(34);
    }];
    
    //股票价格辅助视图
    UIView *middleView = [UIView new];
    [self addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.operationBT.mas_right);
        make.right.equalTo(changeBT.mas_left);
        make.centerY.equalTo(self.nameLB);
    }];
    
    //股票价格
    self.valueLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:28.0] andColor:[YZMAppStyle colorForRiseText]];
    [middleView addSubview:self.valueLB];
    [self.valueLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(middleView);
        make.centerX.equalTo(middleView).offset(-25);
    }];
    
    self.highRiskLB = [UILabel new];
    self.highRiskLB.textColor = [YZMAppStyle colorForMain];
    self.highRiskLB.font = [YZMAppStyle smallSizeFont];
    self.highRiskLB.backgroundColor = [YZMAppStyle colorForGrayUnable];
    self.highRiskLB.layer.cornerRadius = 1;
    self.highRiskLB.textAlignment = NSTextAlignmentCenter;
    [middleView addSubview:self.highRiskLB];
    [self.highRiskLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.valueLB.mas_right).offset(6);
        make.centerY.equalTo(self.valueLB);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(40);
    }];
    
    //股票代码
    self.codeLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForLightGrayText]];
    [self addSubview:self.codeLB];
    [self.codeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLB);
        make.top.equalTo(self.nameLB.mas_bottom).offset(6);
    }];
    
    //股票增量
    self.changeLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForRiseText]];
    [self addSubview:self.changeLB];
    [self.changeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.valueLB);
        make.centerY.equalTo(self.codeLB);
        make.bottom.equalTo(self).offset(-15);
    }];
    
    UIView *bottomLineView = [YZMAppStyle splitLine];
    [self addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.bottom.equalTo(self);
    }];

    self.nameLB.text = @"--";
    self.codeLB.text = @"--";
    self.operationBT.selected = [[YZMyStocksService sharedService] isStockExist:@"--"];
    
    self.valueLB.text = @"--";
    self.changeLB.text = @"--   --";
}

@end
